/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.solestac.view;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import org.colswe.solestac.VO.RequestVO;
import org.colswe.solestac.VO.StudentVO;
import org.colswe.solestac.control.RequestControl;

/**
 *
 * @author solid
 */
public class requestTable extends javax.swing.JPanel {

    
    /**
     * Creates new form requestTable
     */
    private RequestControl requestControl;
    private StudentVO student;
    private Principal parent;
    public requestTable(Principal principal,StudentVO student) {
        this.student = student;
        this.parent = principal;
        requestControl = new RequestControl();
        initComponents();
        setTable();
    }
    
    private String getDateTime(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
       
        return dateFormat.format(date);
    }
    
    
    private void setTable(){
        List<RequestVO> requests = requestControl.searchByStudent(student.getDocument());
        if(requests != null && !requests.isEmpty()){
           int rows = requests.size();
           Object[][] tableModel = new Object[rows][5];
           for (int i = 0; i < rows; i++){
               tableModel[i][0] = requests.get(i).getId();
               tableModel[i][1] = requests.get(i).getRequestType();
               tableModel[i][2] = requests.get(i).getDate();
               tableModel[i][3] = requests.get(i).getAcademicPeriod();
               tableModel[i][4] = requests.get(i).getInstance();               
           }
           
           requestTable.setModel(new DefaultTableModel(tableModel,new String[]{"Id","Tipo","Fecha","Periodo","Instancia Actual"}));
           
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        requestTable = new javax.swing.JTable();
        okButton = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setFont(new java.awt.Font("Bitstream Vera Sans", 0, 16)); // NOI18N
        jLabel1.setText("Solicitudes del Estudiante");

        requestTable.setFont(new java.awt.Font("Bitstream Vera Sans", 0, 16)); // NOI18N
        requestTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Tipo", "Fecha", "Periodo", "Instancia Actual"
            }
        ));
        jScrollPane1.setViewportView(requestTable);

        okButton.setText("Aceptar");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 603, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(300, 300, 300)
                        .addComponent(okButton)))
                .addContainerGap(66, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(46, 46, 46)
                .addComponent(okButton)
                .addContainerGap(414, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        parent.createRequest();
    }//GEN-LAST:event_okButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton okButton;
    private javax.swing.JTable requestTable;
    // End of variables declaration//GEN-END:variables
}
